package org.jeecg.modules.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.CwJbszqkzb;
import org.jeecg.modules.cw.entity.CwJbszqkzbZhu;
import org.jeecg.modules.cw.entity.CwSyrygzbTj;
import org.jeecg.modules.cw.entity.CwSyrygzb;
import org.jeecg.modules.cw.mapper.CwSyrygzbMapper;
import org.jeecg.modules.cw.mapper.CwSyrygzbTjMapper;
import org.jeecg.modules.cw.service.ICwJbszqkzbService;
import org.jeecg.modules.cw.service.ICwJbszqkzbZhuService;
import org.jeecg.modules.cw.service.ICwSyrygzbTjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 赡养人员工资
 * @Author: jeecg-boot
 * @Date:   2025-09-03
 * @Version: V1.0
 */
@Service
public class CwSyrygzbTjServiceImpl extends ServiceImpl<CwSyrygzbTjMapper, CwSyrygzbTj> implements ICwSyrygzbTjService {

	@Autowired
	private CwSyrygzbTjMapper cwSyrygzbTjMapper;
	@Autowired
	private CwSyrygzbMapper cwSyrygzbMapper;
	@Autowired
	private ICwJbszqkzbService cwJbszqkzbService;
	@Autowired
	private ICwJbszqkzbZhuService cwJbszqkzbZhuService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwSyrygzbTj cwSyrygzbTj, List<CwSyrygzb> cwSyrygzbList) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		cwSyrygzbTjMapper.insert(cwSyrygzbTj);
		if(cwSyrygzbList!=null && cwSyrygzbList.size()>0) {
			for(CwSyrygzb entity:cwSyrygzbList) {
				entity.setUnit(cwSyrygzbTj.getUnit());
				entity.setYear(cwSyrygzbTj.getYear());
				entity.setUserId(user.getId());
				//外键设置
				entity.setMainId(cwSyrygzbTj.getId());
				cwSyrygzbMapper.insert(entity);
			}
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwSyrygzbTj cwSyrygzbTj,List<CwSyrygzb> cwSyrygzbList) {
		cwSyrygzbTjMapper.updateById(cwSyrygzbTj);
		Double acz=0.0;
		Double afcz=0.0;
		//1.先删除子表数据
		cwSyrygzbMapper.deleteByMainId(cwSyrygzbTj.getId());
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//2.子表数据重新插入
		if(cwSyrygzbList!=null && cwSyrygzbList.size()>0) {
			for(CwSyrygzb entity:cwSyrygzbList) {
				if ("财政".equals(entity.getCzFcz())){
					acz+=entity.getLivingAllowance();
				}else {
					afcz+=entity.getLivingAllowance();
				}
				//外键设置
				entity.setMainId(cwSyrygzbTj.getId());
				cwSyrygzbMapper.insert(entity);
			}
		}
//		LambdaQueryWrapper<CwJbszqkzb> wrapper = new LambdaQueryWrapper<>();
//		wrapper.eq(CwJbszqkzb::getYear,cwSyrygzbTj.getYear())
//				.eq(CwJbszqkzb::getUnit,cwSyrygzbTj.getUnit())
//				.in(CwJbszqkzb::getProject,"生活补助");
//		long count = cwJbszqkzbService.count(wrapper);
//		if (count == 1) {
//			CwJbszqkzb one1 = cwJbszqkzbService.getOne(wrapper);
//			one1.setUnit(cwSyrygzbTj.getUnit());
//			one1.setYear(cwSyrygzbTj.getYear());
//			one1.setFinancial(acz);
//			one1.setNonFinancials(afcz);
//			if (one1.getYsshj()==null){
//				one1.setYsshj(0.0);
//			}
//			one1.setYsshj(acz+afcz);
//			one1.setCwPx(57);
//			cwJbszqkzbService.updateById(one1);
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwSyrygzbMapper.deleteByMainId(id);
		cwSyrygzbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwSyrygzbMapper.deleteByMainId(id.toString());
			cwSyrygzbTjMapper.deleteById(id);
		}
	}
	
}
